/**
 * 
 */
package lu.med.praxiswahl.wahlumed.client.briefe;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableColumn;
import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;

/**
 * @author PatrickPierre
 * 
 */
public class EntryTablePanel extends JScrollPane {

	private LetterModel model;
	private ColumnWidthControler c;
	private boolean isColumnWidthChanged;
	private TableColumn resizedColumn;

	public EntryTablePanel(final LetterModel model) {

		this.model = model;

		table = new JTable();

		table.setModel(model);
		table.setTableHeader(table.getTableHeader());
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setSelectionModel(table.getSelectionModel());
		table.setRowHeight(60);

		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		table.setDefaultEditor(Object.class, new LetterCellEditor(model));
		table.setDefaultRenderer(Object.class, new LetterCellRenderer(model));
		
		this.setViewportView(table);
		this.setAutoscrolls(true);

		resizedColumn = null;

		c = new ColumnWidthControler(Main.COLUMNWIDTH_CONFIGURATION_FILE,
				"lettertbl", table);

		for (int i = 0; i < 7; i++) {
			resizedColumn = table.getColumnModel().getColumn(i);
			resizedColumn.setPreferredWidth(c.getColumnWidth(i));
		}

		table.getColumnModel().addColumnModelListener(
				new TableColumnWidthListener(this));
		table.getTableHeader().addMouseListener(
				new TableHeaderMouseListener(this, c));

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 6917238014566058657L;
	private JTable table;

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}

	public boolean getColumnWidthChanged() {
		return isColumnWidthChanged;
	}

	public void setColumnWidthChanged(boolean widthChanged,
			TableColumn resizedColumn) {
		isColumnWidthChanged = widthChanged;
		this.resizedColumn = resizedColumn;
	}

	public TableColumn getResizedColumn() {
		return resizedColumn;
	}

	public LetterModel getModel() {
		return model;
	}

}
